package {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import org.zengrong.assets.Assets;
	import org.zengrong.cvsfile.CSV;
	import org.zengrong.net.HTTPLoader;
	import org.zengrong.net.VisualLoader;
	import org.zengrong.utils.ImageDecoder;
	import org.zengrong.utils.MathUtil;
	import org.zengrong.utils.ObjectUtil;
	
	public class Test extends Sprite {
		private var csv:CSV;
		private var visual:VisualLoader;
		
		public function Test() {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			//loaderAssert();
			//http_init();
			var obj:Object = {name:"司马迁",age:"123"}
			//trace(ObjectUtil.obj2String(obj));
		}
		
		/**
		 * 可视资源 
		 * 
		 */		
		private function loaderAssert():void{
			visual = new VisualLoader();
			visual.addEventListener(Event.COMPLETE,complete);
			visual.load("assert/pic.jpg","jpg");
		}
		private function complete(e:Event):void{
			var login:BitmapData = visual.getBitmapData();
			this.addChild(new Bitmap(login));
			visual.destroy();
		}
		
		/**
		 * ImageDecoder 
		 * 
		 */		
		private function imgByte():void{
			var loader:URLLoader = new URLLoader();
			loader.dataFormat =  URLLoaderDataFormat.BINARY;
			loader.addEventListener(Event.COMPLETE,img);
			loader.load(new URLRequest("assert/my.jpg"));
		}
		private function img(e:Event):void{
			var byte:ByteArray =(e.target.data);
			trace(byte.length)
			
			var decoder:ImageDecoder = new ImageDecoder(byte,callback);
		}
		private function callback(bit:Bitmap):void{
			trace(bit.width,bit.height);
			this.addChild(bit);
		}
		
		/**
		 * 处理文件 
		 * 
		 */		
		private function http_init():void{
			var loader:HTTPLoader = new HTTPLoader(completeHandler,errorHandler);
			var arr:Array =["assert/pic.xml","assert/test.txt"]
			var url:String = "assert/test.txt";
			loader.load(url,{name:"yu"});
			loader.addReturnVar("sex","man");
		}
		private function completeHandler(obj:*):void{
			trace(obj is Array)
			for (var item:* in obj){
				trace("属性:" + item + "; \t值："  + obj[item])
			}
			/*for(var k:int=0; k<obj.length;k++){
			for(var i:* in obj[k]){
			trace("属性：" + i,"值:" + obj[k][i]);
			}
			}*/
		}
		private function errorHandler(obj:*):void{
			trace(obj.message)
		}
		private function csvHandler():void{
			csv = new CSV();
			csv.loadURL("test.csv");
			csv.addEventListener(Event.COMPLETE,handler);
			
			/*var loader:URLLoader = new URLLoader(new URLRequest("test.csv"));
			loader.dataFormat = URLLoaderDataFormat.BINARY;
			loader.addEventListener(Event.COMPLETE,handler);*/
		}
		private function handler(e:Event):void{
			trace(csv.getRandomRecords(2))
		}
	}
}